<?php


/**
* Puree CMS
* http://www.molotsoft.ru
*
* @package Puree_CMS
* @subpackage Admin/Components
* @author Ivan Chistyakov <ivan@molotsoft.ru>
* @version $Id: client_methods.php 1725 2010-09-29 13:55:58Z zetruger@gmail.com $
* @link $HeadURL: http://puree-cms.googlecode.com/svn/trunk/plugins/admin/controllers/components/client_methods.php $
*/


class ClientMethodsComponent extends Object {

/******************************************************************************/

var $components = array(
	'Admin.AutomateMethods'
);

/******************************************************************************/

var $Controller = NULL;

var $settings = array();

/******************************************************************************/

function initialize(&$Controller, $settings = array()) {
	$this->Controller = $Controller;
	$this->settings = (array)$settings;
}

function startup(&$Controller) {
	if (!(bool)Configure::read('admin')) {
		$this->setContactRow();
		$this->setSettingRow();
		$this->setStaticTextRow();
		$this->loadStaticPage();
		$this->loadUrlRule();
	}
}

/******************************************************************************/

function setContactRow($id = 1) {
	$ctrl = &$this->Controller;
	if (App::import('Model', 'Contact')) {
		$ctrl->loadModel('Contact');
		$cache_key = 'Contact.byid' . $id . '.' . Configure::read('Config.language');
		$ctrl->set('contact_row', $contact_row = Cache::read($cache_key));
		if (!(bool)$contact_row) {
			$ctrl->set('contact_row', $contact_row = $ctrl->Contact->find('byid', array('value' => $id)));
			Cache::write($cache_key, $contact_row);
		}
		return $contact_row;
	}
	return FALSE;
}

/******************************************************************************/

function setSettingRow($id = 1) {
	$ctrl = &$this->Controller;
	if (App::import('Model', 'Setting')) {
		$ctrl->loadModel('Setting');
		$cache_key = 'Setting.byid' . $id . '.' . Configure::read('Config.language');
		$ctrl->set('setting_row', $setting_row = Cache::read($cache_key));
		$ctrl->set('setting', $setting_row);
		if (!(bool)$setting_row) {
			$ctrl->set('setting_row', $setting_row = $ctrl->Setting->find('byid', array('value' => $id)));
			$ctrl->set('setting', $setting_row);
			Cache::write($cache_key, $setting_row);
		}
		$this->AutomateMethods->processMetaSEO($setting_row, 'Setting');
		return $setting_row;
	}
	return FALSE;
}

/******************************************************************************/

function setStaticTextRow($id = NULL) {
	$ctrl = &$this->Controller;
	if (App::import('Model', 'StaticText')) {
		$ctrl->loadModel('StaticText');
		$cache_key = 'StaticText.byid' . $id . '.' . Configure::read('Config.language');
		$ctrl->set('static_text_row', $static_text_row = Cache::read($cache_key));
		if (!(bool)$static_text_row) {
			$ctrl->set('static_text_row', $static_text_row = $ctrl->StaticText->find('byid', array('value' => $id)));
			Cache::write($cache_key, $static_text_row);
		}
		$this->AutomateMethods->processMetaSEO($static_text_row, 'StaticText');
		// caption
		if (isset($static_text_row[$ctrl->StaticText->name][$ctrl->StaticText->displayField])) {
			$ctrl->PageTitle->dataCaption = $static_text_row[$ctrl->StaticText->name][$ctrl->StaticText->displayField];
		}
		return $static_text_row;
	}
	return FALSE;
}

/******************************************************************************/

function loadStaticPage() {
	$ctrl = $this->Controller;
	if (App::import('Model', 'StaticPage')) {
//
	$ctrl->loadModel($model = 'StaticPage');
	$url = !empty($ctrl->base) ? preg_replace('~^' . preg_quote($ctrl->base) . '~', '', $ctrl->here) : $ctrl->here;
	$route = $ctrl->params['controller'] . '/' . $ctrl->params['action'];
	$ctrl->set('static_page_row', $row = $this->data = $ctrl->StaticPage->find('first', array('conditions' => array('or' => array($model . '.url' => $url, $model . '.route' => $route)))));
	// row
	if (is_array($row) && array_key_exists($model, $row) && is_array($model_data = $row[$model])) {
		// caption
		$display_field = $ctrl->StaticPage->displayField;
		if (array_key_exists($display_field, $model_data)) $ctrl->pageCaption = $model_data[$display_field];
		// meta / seo
$this->AutomateMethods->processMetaSEO($row, 'StaticPage');
if (isset($row[$ctrl->StaticPage->name][$ctrl->StaticPage->displayField])) {
$ctrl->PageTitle->dataCaption = $row[$ctrl->StaticPage->name][$ctrl->StaticPage->displayField];
}
	}
//
	}
	return FALSE;
}

/******************************************************************************/

function loadUrlRule() {
	$ctrl = $this->Controller;
	if (App::import('Model', 'UrlRule')) {
//
	$ctrl->loadModel('UrlRule');
	// urls
	$here = !empty($ctrl->base) ? preg_replace('~^' . preg_quote($ctrl->base) . '~i', '', $ctrl->here) : $ctrl->here;
	$args = preg_split('~/+~', trim($here, '/'), -1, PREG_SPLIT_NO_EMPTY);
	$urls = array('/' . implode('/', $args));
	while (count($args) > 0) {
		$args[count($args) - 1] = '*';
		$urls[] = '/' . implode('/', $args);
		array_pop($args);
	}
	$urls[] = '*';
	// query
	$conditions = array('UrlRule.url' => $urls);
	$order = array('UrlRule.url' => 'ASC', 'UrlRule.id' => 'ASC'); // important priority
	$query = array('conditions' => $conditions, 'order' => $order);
	$rows = $ctrl->UrlRule->find('all', $query);
	// data
	$url_rule_data = array();
	if (is_array($rows)) foreach ($rows as $i => $row) {
		if (is_array($row) && array_key_exists('UrlRule', $row) && is_array($model_data = $row['UrlRule'])) {
			$url_rule_data = array_merge(array_filter($url_rule_data), array_filter($model_data));
		}
	}
	$this->modelData = $url_rule_data;
	$ctrl->set('url_rule', $url_rule_data);
	Configure::write('UrlRule', $url_rule_data);
	// meta / seo
$this->AutomateMethods->processMetaSEO(array('UrlRule' => $row), 'UrlRule');
//
	}
	return FALSE;
}

/******************************************************************************/

function buildRubricRowContain() {
	$ctrl = &$this->Controller;
	return $ctrl->RubricModel->buildContain(); // must improve
}

function setRubricRow($rubric_id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->RubricModel) && $ctrl->RubricModel->isAutomated() && $ctrl->RubricModel->idExists($rubric_id)) {
		$ctrl->set('rubric_id', $rubric_id);
		$ctrl->set('rubric_row', $rubric_row = $ctrl->RubricModel->find('byid', array('value' => $rubric_id, 'contain' => $this->buildRubricRowContain())));
		$this->AutomateMethods->processMetaSEO($rubric_row, 'RubricModel');
		// caption
		if (isset($rubric_row[$ctrl->RubricModel->name][$ctrl->RubricModel->displayField])) {
			$ctrl->PageTitle->dataCaption = $rubric_row[$ctrl->RubricModel->name][$ctrl->RubricModel->displayField];
		}
		return $rubric_row;
	}
	return FALSE;
}

function setRubricPathnodes($rubric_id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->RubricModel) && $ctrl->RubricModel->isAutomated() && $ctrl->RubricModel->isTree() && $ctrl->RubricModel->idExists($rubric_id)) {
		$ctrl->set('rubric_parent_id', $rubric_parent_id = $ctrl->RubricModel->getParentId($rubric_id));
		$ctrl->set('rubric_pathnodes', $rubric_pathnodes = $ctrl->RubricModel->find('pathnodes', array('value' => $rubric_id, 'include_self' => TRUE)));
		return $rubric_pathnodes;
	}
	return FALSE;
}

function setRubricNeighbors($rubric_id = NULL, $query = array()) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->RubricModel) && $ctrl->RubricModel->isAutomated() && $ctrl->RubricModel->idExists($rubric_id)) {
		$ctrl->set('rubric_neighbors', $rubric_neighbors = $ctrl->RubricModel->find('myneighborsdate', Set::merge(array('value' => $rubric_id), (array)$query)));
		return $rubric_neighbors;
	}
	return FALSE;
}

function buildRowsContain() {
	$ctrl = &$this->Controller;
	return array_keys($ctrl->Model->belongsTo); // must improve
}

function buildRowsQuery($rubric_id = NULL) {
	$ctrl = &$this->Controller;
	$query = array('conditions' => array(), 'contain' => $this->buildRowsContain());
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated()) {
		if ($ctrl->Model->hasRubric() && is_object($ctrl->RubricModel) && $ctrl->RubricModel->isAutomated()) {
			$query['conditions'][$ctrl->Model->escapeField($ctrl->Model->getRubricKey())] = $rubric_id;
		}
	}
	return $query;
}

function setRows($rubric_id = NULL, $query = array(), $merge_query = TRUE) {
	$ctrl = &$this->Controller;
	if ($merge_query) {
		$query = Set::merge($this->buildRowsQuery($rubric_id), (array)$query);
	}
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated()) {
		$ctrl->set('total_rows', $total_rows = $ctrl->Model->find('count'));
		$ctrl->set('count_rows', $count_rows = $ctrl->Model->find('count', $query));
		if ($ctrl->Model->isTree()) {
			$ctrl->set('paginated', FALSE);
			$ctrl->set('rows', $rows = $ctrl->data = $ctrl->Model->find('threaded', $query));
		} else {
			if ((bool)$ctrl->Model->paginateLimit) {
				$ctrl->paginate[$ctrl->Model->name] = $query;
				$ctrl->paginate[$ctrl->Model->name]['limit'] = $ctrl->Model->paginateLimit;
				$ctrl->set('paginated', TRUE);
				$ctrl->set('paginate_limit', $ctrl->Model->paginateLimit);
				$ctrl->set('rows', $rows = $ctrl->data = $ctrl->paginate($ctrl->Model->name/*, $query['conditions']*/));
			} else {
				$ctrl->set('paginated', FALSE);
				$ctrl->set('rows', $rows = $ctrl->data = $ctrl->Model->find('all', $query));
			}
		}
		return $rows;
	}
	return FALSE;
}

/******************************************************************************/

function index($rubric_id = NULL, $query = array(), $merge_query = TRUE) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated()) {
		$ctrl->set('rubric_id', $rubric_id);
		if ($ctrl->Model->hasRubric() && is_object($ctrl->RubricModel) && $ctrl->RubricModel->isAutomated()) {
			/*if (!$ctrl->RubricModel->idExists($rubric_id)) {
				$ctrl->redirect($ctrl->RubricModel->getIndexURL());;
			}*/
			$rubric_row = $this->setRubricRow($rubric_id);
			if ($ctrl->RubricModel->isTree()) {
				$rubric_pathnodes = $this->setRubricPathnodes($rubric_id);
			}
			$rubric_neighbors = $this->setRubricNeighbors($rubric_id);
		}
		$rows = $this->setRows($rubric_id, $query, $merge_query);
		// render
		if ($ctrl->autoRender && $ctrl->automateRender) {
			$ctrl->render();
		}
	} else {
		$ctrl->cakeError('error404');
	}
}

/******************************************************************************/

function buildRowContain() {
	$ctrl = &$this->Controller;
	return $ctrl->Model->buildContain(); // must improve
}

function setRow($id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->idExists($id)) {
		$ctrl->set('id', $id);
		$ctrl->set('row', $row = $ctrl->data = $ctrl->Model->find('byid', array('value' => $id, 'contain' => $this->buildRowContain())));
		$this->AutomateMethods->processMetaSEO($row);
		// caption
		if (isset($row[$ctrl->Model->name][$ctrl->Model->displayField])) {
			$ctrl->PageTitle->dataCaption = $row[$ctrl->Model->name][$ctrl->Model->displayField];
		}
		return $row;
	}
	return FALSE;
}

function setPathnodes($id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->isTree() && $ctrl->Model->idExists($id)) {
		$ctrl->set('parent_id', $parent_id = $ctrl->Model->getParentId($id));
		$ctrl->set('pathnodes', $pathnodes = $ctrl->Model->find('pathnodes', array('value' => $id)));
		return $pathnodes;
	}
	return FALSE;
}

function setNeighbors($id = NULL, $query = array()) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->idExists($id)) {
		$ctrl->set('neighbors', $neighbors = $ctrl->Model->find('myneighborsdate', Set::merge(array('value' => $id), (array)$query)));
		return $neighbors;
	}
	return FALSE;
}

/******************************************************************************/

function view($id = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->isAutomated() && $ctrl->Model->idExists($id)) {
		$ctrl->set('id', $id);
		if ($ctrl->Model->hasRubric() && is_object($ctrl->RubricModel) && $ctrl->RubricModel->isAutomated()) {
			/*if (!$ctrl->RubricModel->idExists($rubric_id)) {
				$ctrl->redirect($ctrl->RubricModel->getIndexURL());;
			}*/
			$rubric_row = $this->setRubricRow($rubric_id = $ctrl->Model->getRubricId($id));
			if ($ctrl->RubricModel->isTree()) {
				$rubric_pathnodes = $this->setRubricPathnodes($rubric_id);
			}
			$rubric_neighbors = $this->setRubricNeighbors($rubric_id);
		}
		$row = $this->setRow($id);
		if ($ctrl->Model->isTree()) {
			$pathnodes = $this->setPathnodes($id);
		}
		$neighbors = $this->setNeighbors($id);
		// render
		if ($ctrl->autoRender && $ctrl->automateRender) {
			$ctrl->render();
		}
	} else {
		$ctrl->cakeError('error404');
	}
}

function viewBySlug($id_or_slug = NULL) {
	$ctrl = &$this->Controller;
	if (is_object($ctrl->Model) && $ctrl->Model->hasField('slug')) {
		$id = $ctrl->Model->find('byidorslug', array('value' => $id_or_slug, 'return' => $ctrl->Model->primaryKey));
	} else {
		$id = $id_or_slug;
	}
	$this->view($id);
}

/******************************************************************************/

}


?>